import Router from 'koa-router'
import * as licenseRoutes from './modules/license'
import * as authRoutes from './modules/auth'
import * as uploadRoutes from './modules/upload'

// 路由模块集合
const routeModules = {
    license: licenseRoutes,
    auth: authRoutes,
    upload: uploadRoutes
}

class AppRouter {
    public unprotectedRouter: Router
    public protectedRouter: Router

    constructor() {
        this.unprotectedRouter = new Router()
        this.protectedRouter = new Router()
        this.setupRoutes()
    }

    private setupRoutes() {
        // 设置基础路由
        this.setupBaseRoutes()
        // 注册模块路由
        this.registerModuleRoutes()
    }

    private setupBaseRoutes() {
        this.unprotectedRouter.get('/', async (ctx) => {
            ctx.body = {
                message: 'Welcome to Carpark Backend!'
            }
        })
    }

    private registerModuleRoutes() {
        Object.values(routeModules).forEach((module) => {
            // 注册未受保护的路由
            if (module.unprotectedRouter) {
                this.unprotectedRouter.use(module.unprotectedRouter.routes())
            }

            // 注册受保护的路由
            if (module.protectedRouter) {
                this.protectedRouter.use(module.protectedRouter.routes())
            }
        })
    }
}

// 创建路由实例
const appRouter = new AppRouter()

export const { unprotectedRouter, protectedRouter } = appRouter

// const unprotectedRouter = new Router()
// const protectedRouter = new Router()

// // 基础路由
// unprotectedRouter.get('/', async (ctx) => {
//     ctx.body = {
//         message: 'Welcome to Carpark Backend!'
//     }
// })

// // 注册所有未受保护的路由
// ;[licenseRoutes.unprotectedRouter, authRoutes.unprotectedRouter].forEach((router) => {
//     unprotectedRouter.use(router.routes())
// })

// // 注册所有受保护的路由
// ;[licenseRoutes.protectedRouter, authRoutes.protectedRouter].forEach((router) => {
//     protectedRouter.use(router.routes())
// })

// export { unprotectedRouter, protectedRouter }
